package com.jerry.hm.datastructure.linkedlist;

import java.util.Iterator;

/**
 * 带哨兵的双向链表
 *
 * @author Jerry
 * @since 2024/9/30 9:12
 */
public class DoubleLinkedListSentinel implements Iterable<Integer> {

    /**
     * 头部哨兵
     */
    private Node head;

    /**
     * 尾部哨兵
     */
    private Node tail;

    static class Node {

        /**
         * 上一个节点
         */
        Node pre;

        /**
         * 下一个节点
         */
        Node next;

        /**
         * 节点值
         */
        int value;

        public Node(Node pre, Node next, int value) {
            this.pre = pre;
            this.next = next;
            this.value = value;
        }
    }

    public DoubleLinkedListSentinel() {
        head = new Node(null, null, -1);
        tail = new Node(null, null, -2);
        head.next = tail;
        tail.pre = head;
    }

    /**
     * 获取指定索引的节点
     *
     * @param index
     * @return
     */
    private Node find(int index) {
        int i = -1;

        for (Node cur = head; cur != tail; cur = cur.next, i++) {
            if (i == index) {
                return cur;
            }
        }

        return null;
    }

    /**
     * 获取指定索引的元素值
     *
     * @param index
     * @return
     */
    public int get(int index) {
        if (index < 0) {
            throw illegalException(index);
        }
        Node target = find(index);
        if (target == null) {
            throw illegalException(index);
        }
        return target.value;
    }

    /**
     * 向指定索引插入元素
     *
     * @param index
     * @param value
     */
    public void insert(int index, int value) {
        if (index < 0) {
            throw illegalException(index);
        }
        if (index == 0) {
            addFirst(value);
            return;
        }
        Node pre = find(index - 1);
        if (pre == null) {
            throw illegalException(index);
        }
        Node target = new Node(pre, pre.next, value);
        pre.next = target;
        pre.next.pre = target;
    }

    /**
     * 向头部添加元素
     *
     * @param value
     */
    public void addFirst(int value) {
        insert(0, value);
    }

    /**
     * 移除指定索引的元素
     *
     * @param index
     */
    public void remove(int index) {
        if (index < 0) {
            throw illegalException(index);
        }
        Node pre = find(index - 1);
        if (pre == null) {
            throw illegalException(index);
        }
        Node target = pre.next;
        if (target == tail) {
            throw illegalException(index);
        }
        pre.next = target.next;
        target.next.pre = pre;
    }

    /**
     * 移除第一个元素
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 向尾部添加元素
     *
     * @param value
     */
    public void addLast(int value) {
        Node target = new Node(tail.pre, tail, value);
        tail.pre.next = target;
        tail.pre = target;
    }

    /**
     * 移除最后一个元素
     */
    public void removeLast() {
        Node target = tail.pre;
        if (target == head) {
            throw illegalException(0);
        }
        target.pre.next = tail;
        tail.pre = target.pre;
    }

    @Override
    public Iterator<Integer> iterator() {

        return new Iterator<>() {

            Node cur = head.next;

            @Override
            public boolean hasNext() {
                return cur != tail;
            }

            @Override
            public Integer next() {
                int value = cur.value;
                cur = cur.next;
                return value;
            }
        };
    }

    /**
     * 抛异常
     *
     * @param index
     * @return
     */
    private static RuntimeException illegalException(int index) {
        throw new IllegalArgumentException(String.format("index [%d] 不合法%n", index));
    }
}
